package com.object.module.didi.frontendcontrolller;

import com.object.configuration.utils.PasswordEncryp;
import com.object.configuration.utils.Q;
import com.object.module.didi.entity.DdUserEntity;
import com.object.module.didi.service.DdUserService;
import com.wf.captcha.ArithmeticCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 用户
 *
 * @author 小🌤
 * @email 1578442339@qq.com
 * @date 2021-10-30 15:57:53
 */
@RestController
@RequestMapping("/didi/user")
public class FrontendDdUserController {
    @Autowired
    private DdUserService ddUserService;
    /**
     * 验证码
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @GetMapping("/captcha")
    public Q captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //算术验证码 数字加减乘除. 建议2位运算就行:captcha.setLen(2);
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 40);
        captcha.setLen(2);
        // 获取运算的结果
        String result = captcha.text();
        request.getSession().setAttribute("captcha", result);
        String captchas = captcha.toBase64();
        String encryp = PasswordEncryp.getPaawordEncryp(result);
        return Q.ok().put("captcha", captchas).code(200).put("encrypts",encryp);
    }

    /**
     * 登录判断
     *
     * @param userEntity
     * @return
     */
    @PostMapping("/login")
    public Q login(@RequestBody DdUserEntity userEntity, HttpServletRequest request) {
        DdUserEntity userEntity1 = null;
        if (PasswordEncryp.IfPassowrd(userEntity.getUrCaptcha(),userEntity.getEncrypts())) {
            userEntity1 = ddUserService.login(userEntity);
        }
        if (userEntity1 == null) {
            return Q.error().message("登录失败检查你的验证码或者是密码错误!");
        }
        request.getSession().removeAttribute("captcha");
        return Q.ok().put("user", userEntity1).message("登录成功!");
    }


    /**
     * id 查询信息
     */
    @RequestMapping("/info/{urId}")
    public Q info(@PathVariable("urId") Integer urId) {
        DdUserEntity ddUser = ddUserService.getById(urId);
        return Q.ok().put("ddUser", ddUser);
    }

    /**
     * 注册
     */
    @RequestMapping("/save")
    public Q save(@RequestBody DdUserEntity ddUser, HttpServletRequest request) {
        Q q = ddUserService.saveUser(ddUser);
        return q;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Q update(@RequestBody DdUserEntity ddUser) {
        if (ddUser.getUrQq() != null) {
            ddUser.setUrAvatar("https://q.qlogo.cn/headimg_dl?dst_uin=" + ddUser.getUrQq() + "&spec=100");
        }
        ddUserService.updateById(ddUser);
        return Q.ok();
    }

    /**
     *   修改密码
     */
    @PostMapping("/password")
    public Q password(@RequestBody DdUserEntity user){
        boolean flag= ddUserService.updaatePassword(user);
        return flag?Q.ok().message("密码修改成功"):Q.error().message("密码修改失败 ");
    }


}
